package com.teachoa.bean;

import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.teachoa.constant.Global;
import com.teachoa.enumtype.UserTypeEnum;
import com.teachoa.util.IDUtil;
import com.teachoa.util.MD5Util;
import com.teachoa.util.Maps;

@SuppressWarnings("unchecked")
public class User extends Model<User> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -883535354303177834L;
	
	public static final User dao = new User();
	
	public User checkUser(String username, String password) {
		String sql = "select * from user where loginname = ? and password = ?";
		return dao.findFirst(sql, new Object[]{username, password});
	}
	
	public Page<User> findUserByPage(int pageNo) {
		return dao.paginate(pageNo, Global.pageSize, "select *", "from user order by create_time desc");
	}
	
	public User findUserByUserName(String name) {
		return dao.findFirst("select * from user where username = ?", new Object[]{name});
	}
	
	public Page<User> getUserPageByType(int pageNo, int type) {
		return dao.paginate(pageNo, Global.pageSize, "select *", "from user where type = ?"
				+ " order by create_time desc", new Object[]{type});
	}
	
	public Page<User> getStudentsByTeacherId(int pageNo, String teacherId) {
		return dao.paginate(pageNo, Global.pageSize, "select a.*", " from user a left join teacher_student b on a.id = b.student_id "
				+ "where b.teacher_id = ?", new Object[]{teacherId});
	}
	
	public void createUser() {
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "学生1", "type", UserTypeEnum.STUDENT.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "xuesheng1"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "学生2", "type", UserTypeEnum.STUDENT.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "xuesheng2"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "学生3", "type", UserTypeEnum.STUDENT.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "xuesheng3"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "学生4", "type", UserTypeEnum.STUDENT.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "xuesheng4"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师1", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi1"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师2", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi2"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师3", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi3"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师4", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi4"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师5", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi5"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师6", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi6"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师7", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi7"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师8", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi8"));
//		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "专家", "type", UserTypeEnum.EXPERT.getCode()
				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "zhuanjia"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "学生", "type", UserTypeEnum.STUDENT.getCode()
				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "xuesheng"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师", "type", UserTypeEnum.TEACHER.getCode()
				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi"));
		dao.save();
	}

}
